// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

package version

import (
	"fmt"
	"runtime"
)

// KmoptVersion is the semver of KMOPT
type KmoptVersion struct {
	major int
	minor int
	patch int
	name  string
}

// NewKmoptVersion creates a KmoptVersion object
func NewKmoptVersion() *KmoptVersion {
	return &KmoptVersion{
		major: KmoptVerMajor,
		minor: KmoptVerMinor,
		patch: KmoptVerPatch,
		name:  KmoptVerName,
	}
}

// Name returns the alternave name of KmoptVersion
func (v *KmoptVersion) Name() string {
	return v.name
}

// SemVer returns KmoptVersion in semver format
func (v *KmoptVersion) SemVer() string {
	return fmt.Sprintf("%d.%d.%d", v.major, v.minor, v.patch)
}

// String converts KmoptVersion to a string
func (v *KmoptVersion) String() string {
	return fmt.Sprintf("%s %s\n%s", v.SemVer(), v.name, NewKmoptBuildInfo())
}

// KmoptBuild is the info of building environment
type KmoptBuild struct {
	GitHash   string `json:"gitHash"`
	GitRef    string `json:"gitRef"`
	GoVersion string `json:"goVersion"`
}

// NewKmoptBuildInfo creates a KmoptBuild object
func NewKmoptBuildInfo() *KmoptBuild {
	return &KmoptBuild{
		GitHash:   GitHash,
		GitRef:    GitRef,
		GoVersion: runtime.Version(),
	}
}

// String converts KmoptBuild to a string
func (v *KmoptBuild) String() string {
	return fmt.Sprintf("Go Version: %s\nGit Ref: %s\nGitHash: %s", v.GoVersion, v.GitRef, v.GitHash)
}
